/**
 * 速判选择题生成器
 */
import QuestionGeneratorBase from './question-generator-base.js';
import { FRACTION_POOL } from './question-generator-speed-judge.js';

class SpeedJudgeChoiceGenerator extends QuestionGeneratorBase {
    /**
     * 生成速判选择题
     * @param {number} count 题目数量
     * @param {number} optionsCount 每题选项数量
     * @returns {Array} 题目数组
     */
    static generateQuestions(count = 10, optionsCount = 4) {
        const questions = [];
        const usedFractions = new Set();

        for (let i = 0; i < count; i++) {
            // 从分数池中随机选择一个未使用的分数
            let fraction;
            do {
                fraction = FRACTION_POOL[Math.floor(Math.random() * FRACTION_POOL.length)];
            } while (usedFractions.has(`${fraction.numerator}/${fraction.denominator}`) && usedFractions.size < FRACTION_POOL.length);

            usedFractions.add(`${fraction.numerator}/${fraction.denominator}`);

            // 生成选项（包含正确答案和干扰项）
            const correctAnswer = fraction.decimal;
            const options = [this.formatDecimal(correctAnswer)];

            // 生成干扰项
            while (options.length < optionsCount) {
                // 生成在正确答案附近的干扰项
                const offset = (Math.random() - 0.5) * correctAnswer * 0.5; // 50%范围内的偏移
                let distractor = correctAnswer + offset;
                distractor = Math.max(0.01, Math.min(1, distractor)); // 限制在0.01-1范围内

                const formattedDistractor = this.formatDecimal(distractor);

                // 确保干扰项不重复且不等于正确答案
                if (!options.includes(formattedDistractor)) {
                    options.push(formattedDistractor);
                }
            }

            // 打乱选项顺序
            const shuffledOptions = this.shuffleArray(options);
            const correctIndex = shuffledOptions.indexOf(this.formatDecimal(correctAnswer));

            questions.push({
                id: `judge_choice_${Date.now()}_${i}`,
                fraction: `${fraction.numerator}/${fraction.denominator}`,
                correctAnswer: this.formatDecimal(correctAnswer),
                options: shuffledOptions,
                correctIndex: correctIndex,
                userAnswer: null,
                isCorrect: null,
                timeUsed: 0
            });
        }

        return questions;
    }
}

export default SpeedJudgeChoiceGenerator;

